package com.slwu.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.nio.channels.SocketChannel;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NetUtil {
    public NetUtil() {
    }

    public static String normalizeAddress(String address) {
        String[] blocks = address.split("[:]");
        if (blocks.length > 2) {
            throw new IllegalArgumentException(address + " is invalid");
        } else {
            String host = blocks[0];
            int port = 80;
            if (blocks.length > 1) {
                port = Integer.parseInt(blocks[1]);
            }

            return String.format("%s:%d", host, port);
        }
    }

    public static String getLocalAddress(String address) {
        String[] blocks = address.split("[:]");
        if (blocks.length != 2) {
            throw new IllegalArgumentException(address + " is invalid address");
        } else {
            String host = blocks[0];
            int port = Integer.parseInt(blocks[1]);
            return "0.0.0.0".equals(host) ? String.format("%s:%d", getLocalIp(), port) : address;
        }
    }

    public static boolean isPublicAddress(String ip) {
        return !ip.startsWith("127.") && !ip.startsWith("10.") && !ip.startsWith("172.") && !ip.startsWith("192.");
    }

    private static int matchedIndex(String ip, String[] prefix) {
        for(int i = 0; i < prefix.length; ++i) {
            String p = prefix[i];
            if ("*".equals(p)) {
                if (isPublicAddress(ip)) {
                    return i;
                }
            } else if (ip.startsWith(p)) {
                return i;
            }
        }

        return -1;
    }

    public static String getLocalName() {
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            return inetAddress.getCanonicalHostName();
        } catch (UnknownHostException var1) {
            throw new RuntimeException(var1);
        }
    }

    public static String getLocalIp(String ipPreference) {
        if (ipPreference == null || "".equals(ipPreference.trim())) {
            ipPreference = "*>10>172>192>127";
        }

        String[] prefix = ipPreference.split("[> ]+");

        try {
            Pattern pattern = Pattern.compile("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+");
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            String matchedIp = null;
            int matchedIdx = -1;

            while(interfaces.hasMoreElements()) {
                NetworkInterface ni = interfaces.nextElement();
                Enumeration en = ni.getInetAddresses();

                while(en.hasMoreElements()) {
                    InetAddress addr = (InetAddress)en.nextElement();
                    String ip = addr.getHostAddress();
                    Matcher matcher = pattern.matcher(ip);
                    if (matcher.matches()) {
                        int idx = matchedIndex(ip, prefix);
                        if (idx != -1) {
                            if (matchedIdx == -1) {
                                matchedIdx = idx;
                                matchedIp = ip;
                            } else if (matchedIdx > idx) {
                                matchedIdx = idx;
                                matchedIp = ip;
                            }
                        }
                    }
                }
            }

            if (matchedIp != null) {
                return matchedIp;
            } else {
                return "127.0.0.1";
            }
        } catch (Throwable var12) {
            return "127.0.0.1";
        }
    }

    public static String getLocalIp() {
        try {
            return InetAddress.getLocalHost().getHostAddress().toString();
        } catch (UnknownHostException var1) {
            return getLocalIp("*>10>172>192>127");
        }
    }

    public static String remoteAddress(SocketChannel channel) {
        SocketAddress addr = channel.socket().getRemoteSocketAddress();
        return String.format("%s", addr);
    }

    public static String localAddress(SocketChannel channel) {
        SocketAddress addr = channel.socket().getLocalSocketAddress();
        String res = String.format("%s", addr);
        return addr == null ? res : res.substring(1);
    }

    public static Object[] hostPort(String address) {
        Object[] res = new Object[2];
        String[] bb = address.split("[:]", 2);
        if (bb.length > 0) {
            res[0] = bb[0].trim();
        }

        res[1] = 80;
        if (bb.length > 1) {
            res[1] = Integer.valueOf(bb[1]);
        }

        return res;
    }
}
